package com.duoqin.chat.utils;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.duoqin.chat.Chat;
import com.duoqin.common.utils.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ChatConfig {
    public static final int MAX_CHAT_STORAGE_SIZE = 20; // Mb
    public static final int WARNING_CHAT_STORAGE_SIZE = 15; // Mb

    public static final String INTENT_EXTRA_ENABLE_CHAT = "enableChat";

    public static final String KEY_DUOQIN_CHAT = "duoqin_chat";
    public static final String KEY_DUOQIN_CHAT_NOTIFICATION = "duoqin_chat_notification";
    public static final String KEY_DUOQIN_CHAT_NOTIFICATION_SOUND = "duoqin_chat_notification_sound";

    private final static String QN_SERVER_PATH = "http://weixin-voice.duoqin.com/";
    private final static String folder_path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/Android/Messenger/";
    private final static String AUDIO_AMR_SUFFIX = ".amr";

    public static String mRecordingFilePath;

    public static String getRecordingFilePath() {
        mRecordingFilePath = folder_path + System.currentTimeMillis() + AUDIO_AMR_SUFFIX;
        return mRecordingFilePath;
    }

    public static String getReceivingFilePath() {
        return folder_path + System.currentTimeMillis() + AUDIO_AMR_SUFFIX;
    }

    public static String getMessageFolderPath() {
        return folder_path;
    }

    public static String getFavoriteFolderPath() {
        return folder_path + "favorite/";
    }

    public static void init() {
        File folder = new File(getMessageFolderPath());
        if (!folder.exists()) {
            folder.mkdir();
        }
        folder = new File(getFavoriteFolderPath());
        if (!folder.exists()) {
            folder.mkdir();
        }
    }

    public static void deleteRecordingFile() {
        if (mRecordingFilePath != null) {
            File file = new File(mRecordingFilePath);
            if (file.exists()) {
                file.delete();
            }
        }
        mRecordingFilePath = null;
    }

    public static String getServerMediaPath(Context context, Chat chat) {
        return QN_SERVER_PATH + Utils.getDeviceToken(context) + chat.time_stamp;
    }

    public static void freeStorage(final Context context) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (getChatMediaSize() >= MAX_CHAT_STORAGE_SIZE) { // >= 20MB
                    Cursor cursor = Chat.getChatCursor(context.getContentResolver(),
                            Chat.Columns.MESSAGE_TYPE + " != ?",
                            new String[] {Chat.Columns.CHAT_MESSAGE_TYPE_TEXT});
                    if (cursor != null && cursor.moveToFirst()) {
                        List<Chat> chatList = new ArrayList<>();
                        int i = 0;
                        do {
                            chatList.add(new Chat(cursor));
                            i++;
                        } while (cursor.moveToNext() && i <= 10);
                        Log.i("duoqin", "freeStorage delete media " + chatList.size());
                        for (Chat chat : chatList) {
                            if (!TextUtils.isEmpty(chat.media_path)) {
                                File file = new File(chat.media_path);
                                if (file.exists()) {
                                    file.delete();
                                }
                                chat.media_path = ""; // null is empty, "" is expired
                                Chat.update(context.getContentResolver(), Chat.Columns.CONTENT_URI,
                                        chat);
                            }
                        }
                    }
                }
            }
        }).start();
    }

    public static long getChatMediaSize(){
        File folder = new File(getMessageFolderPath());
        long size = 0;
        try {
            File[] fileList = folder.listFiles();
            for (File file : fileList) {
                if (!file.isDirectory()) {
                    size = size + file.length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size/1024/1024; // in MB
    }
}
